package com.svo.weitu;

import android.app.Application;
import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.svo.platform.utils.HttpUtil;
import com.svo.platform.utils.SvoUtil;
import com.svo.weitu.model.db.ResData;
import com.svo.weitu.model.entity.MenuEntity;
import com.svo.weitu.model.push.Utils;
import com.svo.weitu.util.Cons;
import com.svo.weitu.util.MyUtil;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.util.List;

public class App extends Application {
	public static Context context;
	@Override
	public void onCreate() {
		super.onCreate();
		UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
		context = this;
//		Config.DEBUG = true;
		UMShareAPI.get(this);
		PlatformConfig.setWeixin("wxed7d8021ccd063cd", "f9fd4ef871f2d79a2404321c8e0a710f");
		PlatformConfig.setQQZone("100519939", "8d897160dfd9bf7f7639935685c9b942");
		PlatformConfig.setSinaWeibo("4198106490", "0dc94aec1fd6e1113f3b033f0380ad8d", "http://www.baidu.com");
		// CrashException crashException = CrashException.getInstance();
		// crashException.init(getApplicationContext());
		// 请求订阅数据
		if (new ResData(getApplicationContext()).queryMap().size() == 0) {
			if (MyUtil.isNetworkAvailable(getApplicationContext())) {
				String qudao = Utils.getMetaValue(getApplicationContext(), "UMENG_CHANNEL");
				int version = SvoUtil.getVersion(getApplicationContext());
				HttpUtil.get(Cons.gen + "res/get?qudao=" + qudao + "&version=" + version, null,
						new AsyncHttpResponseHandler() {
							@Override
							public void onStart() {
							}
							@Override
							public void onSuccess(String arg0) {
								super.onSuccess(arg0);
								try {
									List<MenuEntity> lists = new Gson().fromJson(arg0, new TypeToken<List<MenuEntity>>() {
                                    }.getType());
									ResData resData = new ResData(getApplicationContext());
									resData.clear();
									resData.insert(lists);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							@Override
							public void onFailure(Throwable arg0, String arg1) {
							}
						});
			}
		}
	}
}
